package echart;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;

import common.Result;
import pojo.chartData;

@WebServlet("/table")
public class TableServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 模拟数据
        List<String> categories = new ArrayList<>();
        List<Integer> values = new ArrayList<>();
        categories.add("May1");
        categories.add("May2");
        categories.add("May3");
        values.add(20);
        values.add(30);
        values.add(40);
        
        // 将数据封装到chartData对象中
        chartData chartData = new chartData();
        chartData.setCategories(categories);
        chartData.setValues(values);

        // 将Result对象转换为JSON格式
        String jsonData = JSON.toJSONString(Result.success(chartData));
        System.out.print(jsonData);

        // 设置响应内容类型为JSON
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        // 将JSON数据写入响应
        response.getWriter().write(jsonData);
    }
}
